home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 677 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  78 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: gets() question
  5. Date: Mon, 08 Jan 96 13:51:56 GMT
  6. Organization: none
  7. Message-ID: <821109116snz@genesis.demon.co.uk>
  8. References: <4cosgf$rir@newsbf02.news.aol.com> <4cqkt8$1quo@news.gate.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4cqkt8$1quo@news.gate.net> bhutto@gate.net "William Hutto" writes:
  15.  
  16. >In article <4cosgf$rir@newsbf02.news.aol.com>,
  17.  
  18. >>#include "stdio.h"
  19.  
  20. Should be:
  21.  
  22. #include <stdio.h>
  23.  
  24. if you want to be sure to include the standard system stdio.h header.
  25.  
  26. >>void main(void)
  27.  
  28. Read the FAQ.
  29.  
  30. >>{
  31. >>   int i[5];
  32. >>   char s[3][10];
  33. >>
  34. >>   printf ("Enter s[0]:  ");
  35. >>   gets (s[0]);
  36. >          ^^^^          
  37. >
  38. >Your compiler didn't generate an error?
  39.  
  40. Why should it?
  41.  
  42. >*s* is not an array of pointers to 
  43. >arrays of chars as in argv[].
  44.  
  45. True, s is an array of arrays of char. s[0] is an array of 10 chars.
  46. When used as an rvalue in an expression an array evaluates to a pointer to
  47. its first element i.e. a char * in this case.
  48.  
  49. >Now I remember why I always like being explicit. 
  50. >Try this:
  51. >
  52. >        fgets(&s[0][0],9,stdin);
  53.  
  54. It is certainly better to use fgets rather than gets but you could have
  55. written:
  56.  
  57.          fgets(s[0],sizeof s[0],stdin);
  58.  
  59. s[0] is equivalent to &s[0][0] in this context and is arguably more
  60. readable. The length argument of fgets specifies the size of the array
  61. available in the first argument (which is 10 in this case) i.e. fgets will
  62. read a maximum of N-1 characters from the input stream and then terminate the
  63. sequence in the array with '\0'.
  64.  
  65. >You can use a 2 dimensional char array, except that you have to be complete 
  66. >in your form and s[0] is not. Because fgets() (or gets() for that matter) is 
  67. >looking for an address, you need to oblige, hence the prefix of *&*.
  68.  
  69. You need to read up on how arrays and pointers work in C. There are no such
  70. thing as 2 dimensional arrays, just arrays of arrays. Each element of the
  71. overall array is itself an array and behaves just like any other array.
  72.  
  73. -- 
  74. -----------------------------------------
  75. Lawrence Kirby | fred@genesis.demon.co.uk
  76. Wilts, England | 70734.126@compuserve.com
  77. -----------------------------------------
  78.